//
//  MMUndoManager.m
//  MemeMaker
//
//  Created by Phan Quang Ha on 7/15/13.
//  Copyright (c) 2013 soyo. All rights reserved.
//

#import "MMUndoManager.h"

#define MAX_ITEMS 10

@interface MMUndoManager ()
{
    NSMutableArray *currentStack;
    NSInteger currentIndex;
}
@end

@implementation MMUndoManager

- (id) init
{
    if (self = [super init]) {
        currentStack = [[NSMutableArray alloc] init];
        currentIndex = -1;
    }
    return self;
}

- (void)addObjectToStack:(id)object
{
    if (currentIndex < MAX_ITEMS) {
        while (currentStack.count-1 > currentIndex)
        {
            [currentStack removeLastObject];
        }
        [currentStack addObject:object];
        currentIndex ++;
    }else{
        [currentStack removeObjectAtIndex:0];
        [currentStack addObject:object];
    }
    if (currentIndex == 0) {
        _canUndo = NO;
    }else{
        _canUndo = YES;
    }
    _canRedo = NO;
    
}
- (id)undo
{
    currentIndex --;
    if (currentIndex==0) {
        _canUndo = NO;
    }
    _canRedo = YES;
    return [currentStack objectAtIndex:currentIndex];
}
- (id)redo
{
    currentIndex ++;
    if (currentIndex==currentStack.count-1) {
        _canRedo = NO;
    }
    _canUndo = YES;
    return [currentStack objectAtIndex:currentIndex];
}


@end
